package o;

import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore.Images.Media;
import android.provider.MediaStore.Images.Thumbnails;
import android.provider.MediaStore.Video;
import android.text.TextUtils;
import android.util.LruCache;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import com.bumptech.glide.Glide;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class blj {
    private static blj ˎ;
    private static LruCache<String, List<e>> ᐝ = new LruCache<String, List<e>>(2097152) {
        protected /* synthetic */ int sizeOf(Object obj, Object obj2) {
            return ˋ((String) obj, (List) obj2);
        }

        protected int ˋ(String str, List<e> list) {
            if (cgs.ˋ(list)) {
                return 0;
            }
            int i = 0;
            for (e eVar : list) {
                int i2;
                if (eVar == null || eVar.ˊ == null) {
                    i2 = i;
                } else {
                    i2 = i + (blp.ˊ(eVar.ˊ) / WXMediaMessage.DESCRIPTION_LENGTH_LIMIT);
                }
                i = i2;
            }
            return i;
        }
    };
    private final Object ˊ = new Object();
    private ds<ImageView, g> ˋ = new ds();
    private ExecutorService ˏ;
    private boolean ॱ = false;

    static class a implements ThreadFactory {
        private a() {
        }

        public Thread newThread(@NonNull Runnable runnable) {
            return new Thread(runnable, "local-image:" + System.currentTimeMillis());
        }
    }

    class b extends AsyncTask<c, Void, Object> {
        private final WeakReference<ImageView> ˋ;
        final /* synthetic */ blj ˎ;
        private String ˏ;
        private Context ॱ;

        protected /* synthetic */ Object doInBackground(Object[] objArr) {
            return ˊ((c[]) objArr);
        }

        public b(blj o_blj, ImageView imageView, Context context) {
            this.ˎ = o_blj;
            this.ˋ = new WeakReference(imageView);
            this.ॱ = context;
        }

        protected Object ˊ(c... cVarArr) {
            Object ˊ;
            Bitmap bitmap;
            Exception e;
            if (cVarArr[0] == null) {
                blf.ˋ.ˊ("LocalImageLoader", "doInBackground, params[0] is null!");
                return null;
            }
            c cVar = cVarArr[0];
            this.ˏ = this.ˎ.ॱ(cVar);
            if (TextUtils.isEmpty(this.ˏ)) {
                blf.ˋ.ˊ("LocalImageLoader", "doInBackground, imageKey isEmpty");
                return null;
            }
            List ˎ;
            e eVar;
            synchronized (this.ˎ.ˊ) {
                while (this.ˎ.ॱ && !isCancelled()) {
                    try {
                        this.ˎ.ˊ.wait();
                    } catch (InterruptedException e2) {
                        blf.ˋ.ˊ("LocalImageLoader", "doInBackground, InterruptedException: " + e2.toString());
                    }
                }
            }
            try {
                ˊ = this.ˎ.ˊ(this.ॱ, cVar);
                try {
                    if (ˊ instanceof Bitmap) {
                        bitmap = (Bitmap) ˊ;
                        if (bitmap != null) {
                            return ˊ;
                        }
                        ˎ = ˎ(this.ˏ, cVar);
                        eVar = new e();
                        eVar.ˊ = bitmap;
                        eVar.ॱ = cVar;
                        ˎ.add(eVar);
                        blj.ᐝ.put(this.ˏ, ˎ);
                        return bitmap;
                    }
                } catch (OutOfMemoryError e3) {
                    blf.ˋ.ˊ("LocalImageLoader", "doInBackground, OutOfMemoryError: ");
                    bitmap = null;
                    if (bitmap != null) {
                        return ˊ;
                    }
                    ˎ = ˎ(this.ˏ, cVar);
                    eVar = new e();
                    eVar.ˊ = bitmap;
                    eVar.ॱ = cVar;
                    ˎ.add(eVar);
                    blj.ᐝ.put(this.ˏ, ˎ);
                    return bitmap;
                } catch (Exception e4) {
                    e = e4;
                    blf.ˋ.ˊ("LocalImageLoader", "doInBackground, Exception: " + e.toString());
                    bitmap = null;
                    if (bitmap != null) {
                        return ˊ;
                    }
                    ˎ = ˎ(this.ˏ, cVar);
                    eVar = new e();
                    eVar.ˊ = bitmap;
                    eVar.ॱ = cVar;
                    ˎ.add(eVar);
                    blj.ᐝ.put(this.ˏ, ˎ);
                    return bitmap;
                }
            } catch (OutOfMemoryError e5) {
                ˊ = null;
                blf.ˋ.ˊ("LocalImageLoader", "doInBackground, OutOfMemoryError: ");
                bitmap = null;
                if (bitmap != null) {
                    return ˊ;
                }
                ˎ = ˎ(this.ˏ, cVar);
                eVar = new e();
                eVar.ˊ = bitmap;
                eVar.ॱ = cVar;
                ˎ.add(eVar);
                blj.ᐝ.put(this.ˏ, ˎ);
                return bitmap;
            } catch (Exception e6) {
                e = e6;
                ˊ = null;
                blf.ˋ.ˊ("LocalImageLoader", "doInBackground, Exception: " + e.toString());
                bitmap = null;
                if (bitmap != null) {
                    return ˊ;
                }
                ˎ = ˎ(this.ˏ, cVar);
                eVar = new e();
                eVar.ˊ = bitmap;
                eVar.ॱ = cVar;
                ˎ.add(eVar);
                blj.ᐝ.put(this.ˏ, ˎ);
                return bitmap;
            }
            bitmap = null;
            if (bitmap != null) {
                return ˊ;
            }
            ˎ = ˎ(this.ˏ, cVar);
            eVar = new e();
            eVar.ˊ = bitmap;
            eVar.ॱ = cVar;
            ˎ.add(eVar);
            blj.ᐝ.put(this.ˏ, ˎ);
            return bitmap;
        }

        private List<e> ˎ(String str, c cVar) {
            List<e> list = (List) blj.ᐝ.get(str);
            if (list == null) {
                return new ArrayList();
            }
            Iterator it = list.iterator();
            while (it.hasNext()) {
                e eVar = (e) it.next();
                if (eVar != null && eVar.ˊ != null && eVar.ॱ != null && cVar.equals(eVar.ॱ)) {
                    it.remove();
                    return list;
                }
            }
            return list;
        }

        protected void onPostExecute(Object obj) {
            try {
                Object obj2;
                if (isCancelled()) {
                    obj2 = null;
                } else {
                    obj2 = obj;
                }
                ImageView ˊ = ˊ();
                if (obj2 == null || ˊ == null) {
                    blf.ˋ.ˊ("LocalImageLoader", "GetApkIconTask onPostExecute, result = " + obj2 + ", imageView = " + ˊ);
                } else if (obj2 instanceof Bitmap) {
                    ˊ.setImageBitmap((Bitmap) obj2);
                } else if (obj2 instanceof String) {
                    Glide.ˏ(this.ॱ).ॱ(new File((String) obj2)).ˏ(ˊ);
                }
            } catch (Throwable e) {
                blf.ˋ.ˊ("LocalImageLoader", "GetApkIconTask onPostExecute error: ", e);
            }
        }

        private ImageView ˊ() {
            ImageView imageView = (ImageView) this.ˋ.get();
            return this == this.ˎ.ॱ(imageView) ? imageView : null;
        }

        protected void onCancelled() {
            synchronized (this.ˎ.ˊ) {
                super.onCancelled();
                this.ˎ.ˊ.notifyAll();
            }
        }
    }

    public static class c {
        private int ʼ = 540;
        private int ʽ;
        private boolean ˊ = false;
        private int ˋ = -1;
        private String ˎ = null;
        private int ˏ = -1;
        private int ॱ = -1;
        private String ॱॱ = "image";
        private int ᐝ = 960;

        public int ˊ() {
            return this.ʽ;
        }

        public void ˏ(int i) {
            this.ʽ = i;
        }

        public void ˎ(int i) {
            this.ॱ = i;
        }

        public void ˊ(int i) {
            this.ˋ = i;
        }

        public void ˋ(int i) {
            this.ˏ = i;
        }

        public void ॱ(boolean z) {
            this.ˊ = z;
        }

        public void ˎ(String str) {
            this.ॱॱ = str;
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj instanceof c) {
                c cVar = (c) obj;
                if (this.ˋ == cVar.ˋ && this.ॱ == cVar.ॱ && this.ˊ == cVar.ˊ) {
                    return true;
                }
            }
            return false;
        }

        public int hashCode() {
            return 1;
        }
    }

    static class d extends BitmapDrawable {
        private WeakReference<b> ˎ;

        public d(Resources resources, Bitmap bitmap, b bVar) {
            super(resources, bitmap);
            this.ˎ = new WeakReference(bVar);
        }

        public b ˎ() {
            return (b) this.ˎ.get();
        }
    }

    static class e {
        public Bitmap ˊ;
        public c ॱ;

        private e() {
        }
    }

    static class g {
        public b ˎ;
        public c ˏ;

        private g() {
        }
    }

    public static synchronized blj ˋ() {
        blj o_blj;
        synchronized (blj.class) {
            if (ˎ == null) {
                ˎ = new blj();
            }
            o_blj = ˎ;
        }
        return o_blj;
    }

    private blj() {
    }

    public void ॱ(boolean z) {
        synchronized (this.ˊ) {
            this.ॱ = z;
            if (!this.ॱ) {
                ॱ();
                this.ˊ.notifyAll();
            }
        }
    }

    public void ॱ(Context context, ImageView imageView, c cVar) {
        if (cVar == null) {
            blf.ˋ.ˊ("LocalImageLoader", "asynLoadLocalImage, imageInfo == null");
            return;
        }
        Object ॱ = ॱ(cVar);
        if (TextUtils.isEmpty(ॱ)) {
            blf.ˋ.ˊ("LocalImageLoader", "asynLoadLocalImage, imageKey == " + ॱ);
            return;
        }
        Bitmap bitmap;
        if (ᐝ != null) {
            List<e> list = (List) ᐝ.get(ॱ);
            if (!cgs.ˋ(list)) {
                for (e eVar : list) {
                    if (eVar != null && eVar.ˊ != null && eVar.ॱ != null && cVar.equals(eVar.ॱ)) {
                        bitmap = eVar.ˊ;
                        break;
                    }
                }
            }
        }
        bitmap = null;
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
            return;
        }
        ˏ(imageView);
        b bVar = new b(this, imageView, context);
        imageView.setImageDrawable(new d(context.getResources(), null, bVar));
        synchronized (this.ˊ) {
            if (this.ॱ) {
                g gVar = new g();
                gVar.ˎ = bVar;
                gVar.ˏ = cVar;
                ॱ(imageView, gVar);
            } else {
                ॱ(bVar, cVar);
            }
        }
    }

    public synchronized void ˎ() {
        this.ˋ.clear();
    }

    private synchronized void ॱ(ImageView imageView, g gVar) {
        if (gVar != null) {
            this.ˋ.put(imageView, gVar);
        }
    }

    private synchronized void ॱ() {
        ds dsVar = new ds(this.ˋ);
        this.ˋ.clear();
        ʻ();
        for (Entry value : dsVar.entrySet()) {
            ((g) value.getValue()).ˎ.executeOnExecutor(this.ˏ, new c[]{r0.ˏ});
        }
    }

    private b ॱ(ImageView imageView) {
        if (imageView != null) {
            Drawable drawable = imageView.getDrawable();
            if (drawable instanceof d) {
                return ((d) drawable).ˎ();
            }
        }
        return null;
    }

    private boolean ˏ(ImageView imageView) {
        b ॱ = ॱ(imageView);
        if (!(ॱ == null || ॱ.isCancelled())) {
            ॱ.cancel(true);
        }
        return true;
    }

    private Object ˊ(Context context, c cVar) {
        if (cVar == null || (cVar.ˊ && cVar.ˏ <= -1)) {
            blf.ˋ.ˊ("LocalImageLoader", "getMediaThumbnail, imageInfo = " + cVar + ", origId < -1");
            return null;
        }
        int ˏ = cVar.ˏ;
        int ˎ = cVar.ॱ;
        int ॱ = cVar.ˋ;
        Object thumbnail;
        if (cVar.ˊ) {
            if ("image".equals(cVar.ॱॱ)) {
                thumbnail = Thumbnails.getThumbnail(context.getContentResolver(), (long) ˏ, 1, null);
            } else if ("video".equals(cVar.ॱॱ)) {
                thumbnail = Video.Thumbnails.getThumbnail(context.getContentResolver(), (long) ˏ, 1, null);
            } else {
                thumbnail = null;
            }
            if (thumbnail != null) {
                thumbnail = ThumbnailUtils.extractThumbnail(thumbnail, ˎ, ॱ, 2);
            }
            if (thumbnail != null) {
                return ˏ(thumbnail, cVar.ˊ());
            }
            return thumbnail;
        }
        thumbnail = cVar.ˎ;
        if (TextUtils.isEmpty(thumbnail)) {
            thumbnail = ˊ(context, ˏ, cVar.ॱॱ);
        }
        if (!TextUtils.isEmpty(thumbnail)) {
            return thumbnail;
        }
        blf.ˋ.ˊ("LocalImageLoader", "getImageThumbnail, not create thumbnail, imagePath = " + thumbnail + ", origId = " + ˏ);
        return null;
    }

    private static Bitmap ˏ(Bitmap bitmap, int i) {
        if (i == 0 || bitmap == null) {
            return bitmap;
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        matrix.postRotate((float) i);
        Bitmap createBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        bitmap.recycle();
        return createBitmap;
    }

    private String ॱ(c cVar) {
        if (cVar == null) {
            return "";
        }
        String valueOf = String.valueOf(cVar.ˏ);
        String ॱॱ = cVar.ˎ;
        if (TextUtils.isEmpty(ॱॱ)) {
            return valueOf;
        }
        return ॱॱ;
    }

    private String ˊ(Context context, int i, String str) {
        Object e;
        Throwable th;
        Cursor cursor = null;
        Cursor query;
        try {
            String str2;
            Uri uri = Media.EXTERNAL_CONTENT_URI;
            String str3 = "_data";
            String[] strArr = new String[]{"_data"};
            String str4 = "_id=?";
            String[] strArr2 = new String[]{String.valueOf(i)};
            if ("video".equals(str)) {
                uri = Video.Media.EXTERNAL_CONTENT_URI;
                strArr = new String[]{"_data"};
                str4 = "_id=?";
                str2 = "_data";
            } else {
                str2 = str3;
            }
            query = context.getContentResolver().query(uri, strArr, str4, strArr2, null);
            if (query != null) {
                try {
                    if (query.getCount() > 0) {
                        query.moveToFirst();
                        str3 = query.getString(query.getColumnIndex(str2));
                        if (query != null) {
                            return str3;
                        }
                        try {
                            query.close();
                            return str3;
                        } catch (Exception e2) {
                            blf.ˋ.ˏ("LocalImageLoader", "close cursor error");
                            return str3;
                        }
                    }
                } catch (Exception e3) {
                    e = e3;
                    try {
                        blf.ˋ.ˊ("LocalImageLoader", "getImagePath, e: " + e);
                        if (query != null) {
                            return null;
                        }
                        try {
                            query.close();
                            return null;
                        } catch (Exception e4) {
                            blf.ˋ.ˏ("LocalImageLoader", "close cursor error");
                            return null;
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        cursor = query;
                        if (cursor != null) {
                            try {
                                cursor.close();
                            } catch (Exception e5) {
                                blf.ˋ.ˏ("LocalImageLoader", "close cursor error");
                            }
                        }
                        throw th;
                    }
                }
            }
            str3 = null;
            if (query != null) {
                return str3;
            }
            query.close();
            return str3;
        } catch (Exception e6) {
            e = e6;
            query = null;
            blf.ˋ.ˊ("LocalImageLoader", "getImagePath, e: " + e);
            if (query != null) {
                return null;
            }
            query.close();
            return null;
        } catch (Throwable th3) {
            th = th3;
            if (cursor != null) {
                cursor.close();
            }
            throw th;
        }
    }

    public void ˏ() {
        try {
            blf.ˋ.ˎ("LocalImageLoader", "thumbnail destroyCache");
            ˎ();
            ᐝ.evictAll();
            ʼ();
        } catch (Throwable e) {
            blf.ˋ.ˊ("LocalImageLoader", "destroy error", e);
        }
    }

    private void ʻ() {
        if (this.ˏ == null || this.ˏ.isShutdown()) {
            this.ˏ = new ThreadPoolExecutor(0, 6, 5, TimeUnit.SECONDS, new LinkedBlockingQueue(), new a());
        }
    }

    private synchronized void ॱ(b bVar, c cVar) {
        ʻ();
        bVar.executeOnExecutor(this.ˏ, new c[]{cVar});
    }

    private synchronized void ʼ() {
        if (this.ˏ != null) {
            this.ˏ.shutdown();
            this.ˏ = null;
        }
    }
}
